<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>${message("admin.coupon.add")}</title>


<link href="${base}/resources/admin/css/amazeui.css" rel="stylesheet" type="text/css" /><link href="${base}/resources/admin/css/common.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${base}/resources/admin/js/jquery.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/jquery.tools.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/jquery.validate.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/jquery.autocomplete.js"></script>
<script type="text/javascript" src="${base}/resources/admin/editor/kindeditor.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/common.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/input.js"></script>
<script type="text/javascript" src="${base}/resources/admin/datePicker/WdatePicker.js"></script>
<style type="text/css">
    .memberRank label, .productCategory label, .brand label, .coupon label {
        min-width: 120px;
        _width: 120px;
        display: block;
        float: left;
        padding-right: 4px;
        _white-space: nowrap;
    }

</style>
<script type="text/javascript">
$().ready(function() {

	var $inputForm = $("#inputForm");
    var $productTable = $("#productTable");
    var $productSelect = $("#productSelect");
    var $deleteProduct = $("a.deleteProduct");
    var $productTitle = $("#productTitle");
	var $isExchange = $("#isExchange");
	var $point = $("#point");
    var productIds = [];

[@flash_message /]

    // 商品选择
    $productSelect.autocomplete("product_select.htm", {
        dataType: "json",
        max: 20,
        width: 600,
        scrollHeight: 300,
        parse: function(data) {
            return $.map(data, function(item) {
                return {
                    data: item,
                    value: item.fullName
                }
            });
        },
        formatItem: function(item) {
            if ($.inArray(item.id, productIds) < 0) {
                return '<span title="' + item.fullName + '">' + item.fullName.substring(0, 50) + '<\/span>';
            } else {
                return false;
            }
        }
    }).result(function(event, item) {
	[@compress single_line = true]
        var trHtml =
            '<tr class="productTr">
                <th>
                    <input type="hidden" name="productIds" value="' + item.id + '" \/>
                    &nbsp;
                <\/th>
                <td>
                    <span title="' + item.fullName + '">' + item.fullName.substring(0, 50) + '<\/span>
                <\/td>
                <td>
                    <a href="javascript:;" class="deleteProduct"><i class="iconfont icon-shequshanchu del-icon" ></i><\/a>
                <\/td>
            <\/tr>';
	[/@compress]
        $productTitle.show();
        $productTable.append(trHtml);
        productIds.push(item.id);
    });

    // 删除商品
    $deleteProduct.live("click", function() {
        var $this = $(this);
        $.dialog({
            type: "warn",
            content: "${message("admin.dialog.deleteConfirm")}",
            onOk: function() {
                var id = parseInt($this.closest("tr").find("input:hidden").val());
                productIds = $.grep(productIds, function(n, i) {
                    return n != id;
                });
                $this.closest("tr").remove();
                if ($productTable.find("tr.productTr").size() <= 0) {
                    $productTitle.hide();
                }
            }
        },260,180);
    });


    // 是否允许积分兑换
	$isExchange.click(function() {
		if ($(this).prop("checked")) {
			$point.prop("disabled", false);
		} else {
			$point.val("").prop("disabled", true);
		}
	});

	$.validator.addMethod("compare",
		function(value, element, param) {
			var parameterValue = $(param).val();
			if ($.trim(parameterValue) == "" || $.trim(value) == "") {
				return true;
			}
			try {
				return parseFloat(parameterValue) <= parseFloat(value);
			} catch(e) {
				return false;
			}
		},
		"${message("admin.coupon.compare")}"
	);

	// 表单验证
	$inputForm.validate({
		rules: {
			name: "required",
			cut: "required",
//			prefix: "required",
			minimumQuantity: "digits",
			maximumQuantity: {
				digits: true,
				compare: "#minimumQuantity"
			},
			minimumPrice: {
				min: 0,
				decimal: {
					integer: 12,
					fraction: ${setting.priceScale}
				}
			},
			maximumPrice: {
				required:true,
				min: 0,
				decimal: {
					integer: 12,
					fraction: ${setting.priceScale}
				},
				compare: "#minimumPrice"
			},
			priceExpression: {
				remote: {
					url: "check_price_expression.htm",
					cache: false
				}
			},
			point: {
				required: true,
				digits: true
			}
		}
	});

});
</script>
</head>
<body>
	<div class="path">
		<a href="${base}/asl-admin/common/index.htm">${message("admin.path.index")}</a> &raquo; ${message("admin.coupon.add")}
	</div>
	<form id="inputForm" action="save.htm" method="post">
		<ul id="tab" class="tab">
			<li>
				<input type="button" value="${message("admin.coupon.base")}" />
			</li>
			[#--<li>
				<input type="button" value="${message("Coupon.introduction")}" />
			</li>--]
		</ul>
		<div class="tabContent">
			<table class="input">
				<tr>
					<th>
						<span class="requiredField">*</span>${message("Coupon.name")}:
					</th>
					<td>
						<input type="text" name="name" class="text" maxlength="200" />
                        <input type="hidden" name="prefix" value="default" class="text" maxlength="200" />
					</td>
				</tr>
				<tr>
					<th>
						${message("Coupon.beginDate")}:
					</th>
					<td>
						<input type="text" id="beginDate" name="beginDate" class="text Wdate" onfocus="WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm:ss', maxDate: '#F{$dp.$D(\'endDate\')}'});" />
					</td>
				</tr>
				<tr>
					<th>
						${message("Coupon.endDate")}:
					</th>
					<td>
						<input type="text" id="endDate" name="endDate" class="text Wdate" onfocus="WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm:ss', minDate: '#F{$dp.$D(\'beginDate\')}'});" />
					</td>
				</tr>
				<tr>
					<th>
                        <span class="requiredField">*</span>满足价格(满):
					</th>
					<td colspan="2">
						<input type="text" name="maximumPrice" class="text" maxlength="16" />
					</td>
				</tr>
                <tr>
                    <th>
                        <span class="requiredField">*</span>优惠价格(减):
                    </th>
                    <td>
                        <input type="text" name="cut" class="text" maxlength="200" />
                    </td>
                </tr>
                <tr>
                    <th>
                        <span class="requiredField">*</span>每个会员可领取数量:
                    </th>
                    <td>
                        <input type="text" name="receivableNum" class="text" value="1" maxlength="200" />
                    </td>
                </tr>
				<tr>
					<th>
						${message("admin.common.setting")}:
					</th>
					<td>
						<label>
							<input type="checkbox" name="isEnabled" value="true" checked="checked" />${message("Coupon.isEnabled")}
							<input type="hidden" name="_isEnabled" value="false" />
						</label>
						<label>
							<input type="checkbox" name="isShare" value="true" checked="checked" />是否微信分享
							<input type="hidden" name="_isShare" value="false" />
						</label>
						<label>
							<input type="hidden" name="_isExchange" value="false" />
						</label>
					</td>
				</tr>
                <tr class="productCategory">
                    <th>
					${message("Promotion.productCategories")}
                    </th>
                    <td colspan="2">
					[#list productCategories as productCategory]
                        <label>
                            <input type="checkbox" name="productCategoryIds" value="${productCategory.id}" />${productCategory.name}
                        </label>
					[/#list]
                    </td>
                </tr>
			</table>
            <table id="productTable" class="input">
                <tr>
                    <th>
					${message("Promotion.products")}:
                    </th>
                    <td colspan="2">
                        <input type="text" id="productSelect" name="productSelect" class="text" maxlength="200" title="${message("admin.promotion.productSelectTitle")}" />
                    </td>
                </tr>
                <tr id="productTitle" class="title hidden">
                    <th>
                        &nbsp;
                    </th>
                    <td width="712">
					${message("Product.name")}
                    </td>
                    <td>
					${message("admin.common.handle")}
                    </td>
                </tr>
            </table>
		</div>
		<div class="tabContent">
			<table class="input">
				<tr>
					<td>
						<textarea id="editor" name="introduction" class="editor" style="width: 100%;"></textarea>
					</td>
				</tr>
			</table>
		</div>
		<table class="input">
			<tr>
				<th>
					&nbsp;
				</th>
				<td>
					<input type="submit" class="button" value="${message("admin.common.submit")}" />
					<input type="button" class="button" value="${message("admin.common.back")}" onclick="location.href='list.htm'" />
				</td>
			</tr>
		</table>
	</form>
</body>
</html>